package io.tec.cloud.algorithm.c08_v20220705;

import java.util.HashMap;

public class Code08_1_TrieTree {

    static class TrieTreeNode {
        TrieTreeNode[] next;
        int p;
        int e;

        public TrieTreeNode() {
            this.next = new TrieTreeNode[26];
            this.p = 0;
            this.e = 0;
        }
    }

    static class Trie1 {
        private TrieTreeNode root;

        public Trie1() {
            root = new TrieTreeNode();
        }


        public void insert(String word) {
            if (word == null) {
                return;
            }

            final char[] chars = word.toCharArray();
            TrieTreeNode node = root;
            node.p++;
            for (char str : chars) {
                final int index = str - 'a';
                if (node.next[index] == null) {
                    node.next[index] = new TrieTreeNode();
                }
                node.next[index].p++;
                node = node.next[index];
            }
            node.e++;
        }

        public void delete(String word) {
            if (search(word) != 0) {
                TrieTreeNode node = root;
                final char[] chars = word.toCharArray();
                for (char str : chars) {
                    final int index = str - 'a';
                    if (--node.next[index].p == 0) {
                        node.next[index] = null;
                        return;
                    }
                    node = node.next[index];
                }
                node.e--;
            }
        }

        public int search(String word) {
            if (word == null) {
                return 0;
            }

            TrieTreeNode node = root;
            final char[] chars = word.toCharArray();
            for (char str : chars) {
                final int index = str - 'a';
                if (node.next[index] == null) {
                    return 0;
                }
                node = node.next[index];
            }
            return node.e;
        }

        public int prefixNumber(String pre) {
            if (pre == null) {
                return 0;
            }

            TrieTreeNode node = root;
            final char[] chars = pre.toCharArray();
            for (char str : chars) {
                final int index = str - 'a';
                if (node.next[index] == null) {
                    return 0;
                }
                node = node.next[index];
            }
            return node.p;
        }
    }

    public static class Right {

        private HashMap<String, Integer> box;

        public Right() {
            box = new HashMap<>();
        }

        public void insert(String word) {
            if (!box.containsKey(word)) {
                box.put(word, 1);
            } else {
                box.put(word, box.get(word) + 1);
            }
        }

        public void delete(String word) {
            if (box.containsKey(word)) {
                if (box.get(word) == 1) {
                    box.remove(word);
                } else {
                    box.put(word, box.get(word) - 1);
                }
            }
        }

        public int search(String word) {
            if (!box.containsKey(word)) {
                return 0;
            } else {
                return box.get(word);
            }
        }

        public int prefixNumber(String pre) {
            int count = 0;
            for (String cur : box.keySet()) {
                if (cur.startsWith(pre)) {
                    count++;
                }
            }
            return count;
        }
    }

    // for test
    public static String generateRandomString(int strLen) {
        char[] ans = new char[(int) (Math.random() * strLen) + 1];
        for (int i = 0; i < ans.length; i++) {
            int value = (int) (Math.random() * 6);
            ans[i] = (char) (97 + value);
        }
        return String.valueOf(ans);
    }

    // for test
    public static String[] generateRandomStringArray(int arrLen, int strLen) {
        String[] ans = new String[(int) (Math.random() * arrLen) + 1];
        for (int i = 0; i < ans.length; i++) {
            ans[i] = generateRandomString(strLen);
        }
        return ans;
    }


    public static void main(String[] args) {
        int arrLen = 100;
        int strLen = 20;
        int testTimes = 100000;
        for (int i = 0; i < testTimes; i++) {
            String[] arr = generateRandomStringArray(arrLen, strLen);
            Trie1 trie1 = new Trie1();
            Right right = new Right();
            for (int j = 0; j < arr.length; j++) {
                double decide = Math.random();
                if (decide < 0.25) {
                    trie1.insert(arr[j]);
                    right.insert(arr[j]);
                } else if (decide < 0.5) {
                    trie1.delete(arr[j]);
                    right.delete(arr[j]);
                } else if (decide < 0.75) {
                    int ans1 = trie1.search(arr[j]);
                    int ans3 = right.search(arr[j]);
                    if (ans1 != ans3) {
                        System.out.println("Oops!");
                        return;
                    }
                } else {
//                    int ans1 = trie1.prefixNumber(arr[j]);
//                    int ans3 = right.prefixNumber(arr[j]);
//                    if (ans1 != ans3) {
//                        System.out.println("Oops!");
//                    }
                }
            }
        }
        System.out.println("finish!");

    }

}
